home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MotifDesktopIconUI.java < prev    next >
Text File  |  1998-06-30  |  13KB  |  389 lines

  1. /*
  2.  * @(#)MotifDesktopIconUI.java    1.7 98/02/05
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.motif;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.*;
  26. import com.sun.java.swing.border.*;
  27. import com.sun.java.swing.plaf.*;
  28. import java.beans.*;
  29. import java.util.EventListener;
  30. import java.io.Serializable;
  31.  
  32.  
  33. /**
  34.  * Motif rendition of the component.
  35.  * <p>
  36.  * Warning: serialized objects of this class will not be compatible with
  37.  * future swing releases.  The current serialization support is appropriate
  38.  * for short term storage or RMI between Swing1.0 applications.  It will
  39.  * not be possible to load serialized Swing1.0 objects with future releases
  40.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  41.  * baseline for the serialized form of Swing objects.
  42.  *
  43.  * @version 1.7 02/05/98
  44.  * @author Thomas Ball
  45.  */
  46. public class MotifDesktopIconUI extends DesktopIconUI implements Serializable
  47. {
  48.     JInternalFrame.JDesktopIcon desktopIcon;
  49.     JInternalFrame frame;
  50.     Icon defaultIcon;
  51.     IconButton iconButton;
  52.     IconLabel iconLabel;
  53.     JPopupMenu systemMenu;
  54.     EventListener mml;
  55.  
  56.     final static Font defaultTitleFont = new Font("SansSerif", Font.PLAIN, 12);
  57.  
  58.     public static ComponentUI createUI(JComponent c)    {
  59.         return new MotifDesktopIconUI();
  60.     }
  61.  
  62.     public MotifDesktopIconUI() {
  63.     }
  64.  
  65. String title;
  66.  
  67.     public void installUI(JComponent c)   {
  68.     desktopIcon = (JInternalFrame.JDesktopIcon)c;
  69.     frame = desktopIcon.getInternalFrame();
  70.         setDefaultIcon(UIManager.getIcon("DesktopIcon.icon"));
  71.         iconButton = new IconButton(defaultIcon);
  72.  
  73.         // An unhanded way of creating a system popup menu.
  74.     MotifInternalFrameTitlePane titlePane = 
  75.             new MotifInternalFrameTitlePane(frame);
  76.         systemMenu = titlePane.getSystemMenu();
  77.  
  78.     iconButton.addActionListener(new ActionListener() {
  79.             public void actionPerformed(ActionEvent e) {
  80.                 systemMenu.show(iconButton, 0, desktopIcon.getHeight());
  81.             }
  82.         });
  83.         iconButton.addMouseListener(new MouseAdapter() {
  84.             public void mousePressed(MouseEvent e) {
  85.                 if (e.getClickCount() == 2) {
  86.                     try {
  87.                         frame.setIcon(false);
  88.                     } catch (PropertyVetoException e2) { }
  89.                     systemMenu.setVisible(false);
  90.                 }
  91.             }
  92.         });
  93.  
  94.         MotifFrameBorder border = new MotifFrameBorder(desktopIcon);
  95.     desktopIcon.setLayout(new BorderLayout());
  96.         iconButton.setBorder(border);
  97.     desktopIcon.add(iconButton, BorderLayout.CENTER);
  98.         iconLabel = new IconLabel();
  99.         iconLabel.setBorder(border);
  100.         desktopIcon.add(iconLabel, BorderLayout.SOUTH);
  101.         desktopIcon.setSize(desktopIcon.getPreferredSize());
  102.         desktopIcon.validate();
  103.  
  104.     mml = new MotionListener();
  105.     desktopIcon.addMouseMotionListener((MouseMotionListener)mml);
  106.     desktopIcon.addMouseListener((MouseListener)mml);
  107.     JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
  108.     }
  109.  
  110.     public void uninstallUI(JComponent c) {
  111.     desktopIcon.setLayout(null);
  112.     desktopIcon.remove(iconButton);
  113.         desktopIcon.remove(iconLabel);
  114.     desktopIcon.removeMouseMotionListener((MouseMotionListener)mml);
  115.     desktopIcon.removeMouseListener((MouseListener)mml);
  116.     desktopIcon = null;
  117.     frame = null;
  118.     }
  119.  
  120.     final static int LABEL_HEIGHT = 18;
  121.     final static int LABEL_DIVIDER = 4;    // padding between icon and label
  122.  
  123.     public Dimension getMinimumSize(JComponent c) {
  124.         JInternalFrame iframe = desktopIcon.getInternalFrame();
  125.         int w = defaultIcon.getIconWidth();
  126.         int h = defaultIcon.getIconHeight() + LABEL_HEIGHT + LABEL_DIVIDER;
  127.  
  128.     Border border = iframe.getBorder();
  129.     if(border != null) {
  130.         w += border.getBorderInsets(iframe).left + 
  131.                 border.getBorderInsets(iframe).right;
  132.         h += border.getBorderInsets(iframe).bottom + 
  133.                 border.getBorderInsets(iframe).top;
  134.         }
  135.  
  136.     return new Dimension(w, h);
  137.     }
  138.  
  139.     public Dimension getPreferredSize(JComponent c) {
  140.         return getMinimumSize(c);
  141.     }
  142.  
  143.     public Dimension getMaximumSize(JComponent c){
  144.         return getMinimumSize(c);
  145.     }
  146.  
  147.     public Insets getInsets(JComponent c) {
  148.         JInternalFrame iframe = desktopIcon.getInternalFrame();
  149.     Border border = iframe.getBorder();
  150.     if(border != null)
  151.         return border.getBorderInsets(iframe);
  152.     
  153.     return new Insets(0,0,0,0);
  154.     }
  155.  
  156.     private class MotionListener 
  157.         extends MouseAdapter implements MouseMotionListener, Serializable
  158.     {
  159.     // _x & _y are the mousePressed location in absolute coordinate system
  160.         int _x, _y;
  161.     // __x & __y are the mousePressed location in source view's coordinate system
  162.     int __x, __y;
  163.         Rectangle startingBounds;
  164.  
  165.         public void mouseReleased(MouseEvent e) {
  166.             _x = 0;
  167.             _y = 0;
  168.             __x = 0;
  169.             __y = 0;
  170.             startingBounds = null;
  171.         }
  172.                 
  173.         public void mousePressed(MouseEvent e) {
  174.             Point p = SwingUtilities.convertPoint((Component)e.getSource(), 
  175.                         e.getX(), e.getY(), null);
  176.             __x = e.getX();
  177.             __y = e.getY();
  178.             _x = p.x;
  179.             _y = p.y;
  180.             startingBounds = desktopIcon.getBounds();
  181.  
  182.             try { frame.setSelected(true); } catch (PropertyVetoException e1) { }
  183.         if(desktopIcon.getParent() instanceof JLayeredPane) {
  184.         ((JLayeredPane)desktopIcon.getParent()).moveToFront(desktopIcon);
  185.          }
  186.  
  187.             if(e.getClickCount() > 1) {
  188.         if(frame.isIconifiable() && frame.isIcon()) {
  189.                     try { frame.setIcon(false); } catch (PropertyVetoException e2) { }
  190.         }
  191.             }
  192.  
  193.      }
  194.  
  195.          public void mouseMoved(MouseEvent e) {}
  196.  
  197.          public void mouseDragged(MouseEvent e) {                                        
  198.             Point p; 
  199.         int newX, newY, newW, newH;
  200.             int deltaX;
  201.             int deltaY;
  202.         Dimension min;
  203.         Dimension max;
  204.             p = SwingUtilities.convertPoint((Component)e.getSource(), 
  205.                                         e.getX(), e.getY(), null);
  206.         
  207.         Insets i = desktopIcon.getInsets();
  208.         int pWidth, pHeight;
  209.         pWidth = ((JComponent)desktopIcon.getParent()).getWidth();
  210.         pHeight = ((JComponent)desktopIcon.getParent()).getHeight();
  211.         
  212.         newX = startingBounds.x - (_x - p.x);
  213.         newY = startingBounds.y - (_y - p.y);
  214.         // Make sure we stay in-bounds
  215.         if(newX + i.left <= -__x)
  216.             newX = -__x - i.left;
  217.         if(newY + i.top <= -__y)
  218.             newY = -__y - i.top;
  219.         if(newX + __x + i.right > pWidth)
  220.             newX = pWidth - __x - i.right;
  221.         if(newY + __y + i.bottom > pHeight)
  222.             newY =  pHeight - __y - i.bottom;
  223.         
  224.         JDesktopPane d;
  225.         if((d = desktopIcon.getDesktopPane()) != null) {
  226.             DesktopManager dm;
  227.             dm = d.getDesktopManager();
  228.             dm.setBoundsForFrame(desktopIcon, newX, newY, 
  229.                          desktopIcon.getWidth(), 
  230.                          desktopIcon.getHeight());
  231.         } else {
  232.             moveAndRepaint(desktopIcon, newX, newY, 
  233.                 desktopIcon.getWidth(), desktopIcon.getHeight());
  234.         }
  235.         return;
  236.     }
  237.  
  238.         public void moveAndRepaint(JComponent f, int newX, int newY, 
  239.                     int newWidth, int newHeight) {
  240.         Rectangle r = f.getBounds();
  241.         f.setBounds(newX, newY, newWidth, newHeight);        
  242.         SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
  243.         f.getParent().repaint(r.x, r.y, r.width, r.height);
  244.         }    
  245.     }; /// End MotionListener
  246.  
  247.     /**
  248.       * Returns the default desktop icon.
  249.       */
  250.     public Icon getDefaultIcon() {
  251.     return defaultIcon;
  252.     }
  253.  
  254.     /**
  255.       * Sets the icon used as the default desktop icon.
  256.       */
  257.     public void setDefaultIcon(Icon newIcon) {
  258.     defaultIcon = newIcon;
  259.     }
  260.  
  261.     class IconLabel extends JPanel {
  262.  
  263.         IconLabel() {
  264.             super();
  265.             setFont(defaultTitleFont);
  266.  
  267.             // Forward mouse events to titlebar for moves.
  268.             addMouseMotionListener(new MouseMotionListener() {
  269.                 public void mouseDragged(MouseEvent e) {
  270.                     forwardEventToParent(e);
  271.                 }
  272.                 public void mouseMoved(MouseEvent e) {
  273.                     forwardEventToParent(e);
  274.                 }
  275.             });
  276.             addMouseListener(new MouseListener() {
  277.                 public void mouseClicked(MouseEvent e) {
  278.                     forwardEventToParent(e);
  279.                 }
  280.                 public void mousePressed(MouseEvent e) {
  281.                     forwardEventToParent(e);
  282.                 }
  283.                 public void mouseReleased(MouseEvent e) {
  284.                     forwardEventToParent(e);
  285.                 }
  286.                 public void mouseEntered(MouseEvent e) {
  287.                     forwardEventToParent(e);
  288.                 }
  289.                 public void mouseExited(MouseEvent e) {
  290.                     forwardEventToParent(e);
  291.                 }
  292.             });
  293.         }
  294.  
  295.         void forwardEventToParent(MouseEvent e) {
  296.             getParent().dispatchEvent(new MouseEvent(
  297.                 getParent(), e.getID(), e.getWhen(), e.getModifiers(),
  298.                 e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
  299.         }
  300.  
  301.         public boolean isFocusTraversable() { 
  302.             return false; 
  303.         }
  304.  
  305.         public Dimension getMinimumSize() {
  306.             return new Dimension(defaultIcon.getIconWidth() + 1,
  307.                                  LABEL_HEIGHT + LABEL_DIVIDER);
  308.         }
  309.  
  310.         public Dimension getPreferredSize() {
  311.             String title = frame.getTitle();
  312.             FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(defaultTitleFont);
  313.             int w = fm.stringWidth(title) + 4;
  314.             return new Dimension(w, LABEL_HEIGHT + LABEL_DIVIDER);
  315.         }
  316.  
  317.         public void paint(Graphics g) {
  318.             super.paint(g);
  319.  
  320.             // touch-up frame
  321.             int maxX = getWidth() - 1;
  322.             Color shadow = 
  323.                 UIManager.getColor("inactiveCaptionBorder").darker().darker();
  324.             g.setColor(shadow);
  325.             g.setClip(0, 0, getWidth(), getHeight());
  326.             g.drawLine(maxX - 1, 1, maxX - 1, 1);
  327.             g.drawLine(maxX, 0, maxX, 0);
  328.  
  329.             // fill background
  330.             g.setColor(UIManager.getColor("inactiveCaption"));
  331.             g.fillRect(2, 1, maxX - 3, LABEL_HEIGHT + 1);
  332.  
  333.             // draw text -- clipping to truncate text like CDE/Motif
  334.             g.setClip(2, 1, maxX - 4, LABEL_HEIGHT);
  335.             int y = LABEL_HEIGHT - g.getFontMetrics().getDescent();
  336.             g.setColor(UIManager.getColor("inactiveCaptionText"));
  337.             g.drawString(frame.getTitle(), 4, y);
  338.         }
  339.     }
  340.  
  341.     class IconButton extends JButton {
  342.         Icon icon;
  343.  
  344.         IconButton(Icon icon) {
  345.             super(icon);
  346.             this.icon = icon;
  347.  
  348.             // Forward mouse events to titlebar for moves.
  349.             addMouseMotionListener(new MouseMotionListener() {
  350.                 public void mouseDragged(MouseEvent e) {
  351.                     forwardEventToParent(e);
  352.                 }
  353.                 public void mouseMoved(MouseEvent e) {
  354.                     forwardEventToParent(e);
  355.                 }
  356.             });
  357.             addMouseListener(new MouseListener() {
  358.                 public void mouseClicked(MouseEvent e) {
  359.                     forwardEventToParent(e);
  360.                 }
  361.                 public void mousePressed(MouseEvent e) {
  362.                     forwardEventToParent(e);
  363.                 }
  364.                 public void mouseReleased(MouseEvent e) {
  365.                     if (!systemMenu.isShowing()) {
  366.                         forwardEventToParent(e);
  367.                     }
  368.                 }
  369.                 public void mouseEntered(MouseEvent e) {
  370.                     forwardEventToParent(e);
  371.                 }
  372.                 public void mouseExited(MouseEvent e) {
  373.                     forwardEventToParent(e);
  374.                 }
  375.             });
  376.         }
  377.  
  378.         void forwardEventToParent(MouseEvent e) {
  379.             getParent().dispatchEvent(new MouseEvent(
  380.                 getParent(), e.getID(), e.getWhen(), e.getModifiers(),
  381.                 e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
  382.         }
  383.  
  384.         public boolean isFocusTraversable() { 
  385.             return false; 
  386.         }
  387.     }
  388. }
  389.